package com.itheima.hotel;

import java.util.*;
import java.util.stream.Collectors;

public class Hotel {
    private boolean has_login;
    private List<Room> rooms;
    private LinkedHashMap<String, List<String>> booking;

    public Hotel() {
        // 初始化一些房间
        this.rooms = new ArrayList<Room>();
        this.booking = new LinkedHashMap<String, List<String>>();
        this.rooms.add(new Room("101", "1", false));
        this.rooms.add(new Room("102", "2", false));
        this.rooms.add(new Room("103", "2", false));
        this.rooms.add(new Room("104", "1", false));
        this.rooms.add(new Room("105", "1", false));
        this.rooms.add(new Room("106", "2", false));
    }

    public boolean isHas_login() {
        return has_login;
    }

    public void setHas_login(boolean has_login) {
        this.has_login = has_login;
    }

    public void login(String username, String password) {
        // 用户名和密码进行登录验证，用户名和密码可以写死在代码中
        if (username.equals("admin") && password.equals("123456")) {
            // 成功登录
            this.setHas_login(true);
        } else {
            System.out.println("用户名或密码不正确！");
        }
    }

    //显示所有房间信息
    public void displayAllRooms() {
        //.....
        if (this.rooms.size() == 0) {
            System.out.println("暂无房间.");
        }
        this.rooms.forEach(room -> System.out.println(room.printRoom()));
    }

    //显示所有已预定房间信息
    public void displayAllReservedRooms() {
        //.....
        List<Room> reservedRoom = this.rooms.stream()
                .filter(Room::isReserved)
                .collect(Collectors.toList());

        if (reservedRoom.size() == 0) {
            System.out.println("暂无预定房间.");
        }

        reservedRoom.forEach(room -> {
            System.out.print(room.printRoom());
            this.booking.forEach((k, v) -> {
                if (v.get(0).equals(room.getRoomNumber())) {
                    System.out.println(" --> 订单号: " + k + " 入住时间: " + v.get(1));
                }
            });
        });
    }

    //按照房间类型排序
    public void sortRoomsByType() {
        this.rooms.stream()
                .sorted((room1, room2) -> room1.getRoomType().compareTo(room2.getRoomType()))
                .forEach(room -> System.out.println(room.printRoom()));
    }

    //按照房间编号排序
    public void sortRoomsByNumber() {
        this.rooms.stream()
                .sorted((room1, room2) -> room1.getRoomNumber().compareTo(room2.getRoomNumber()))
                .forEach(room -> System.out.println(room.printRoom()));
    }

    //预订房间(参数1：房间类型； 参数2：入住日期)
    public void reserveRoom(String roomType, String checkInDate) {

        // 获取还未预定，并且符合类型的房间集合
        List<Room> notReservedRooms = this.rooms.stream()
                .filter(room -> !room.isReserved())
                .filter(room -> room.getRoomType().equals(roomType))
                .collect(Collectors.toList());

        if (notReservedRooms.size() > 0) {
            // 生成订单
            String orderNumber = createOrderNumber(6);
            Room selectedRoom = notReservedRooms.get(0);
            List<String> bookValue = new ArrayList<>();
            bookValue.add(selectedRoom.getRoomNumber());
            bookValue.add(checkInDate);
            this.booking.put(orderNumber, bookValue);
            // 修改房间状态
            selectedRoom.setReserved(true);
            //情况1： 预订成功
            System.out.println("预订成功！订单号为：" + orderNumber + "，入住日期为：" + checkInDate + "，房间信息为：");
            System.out.println(selectedRoom.printRoom());

        } else {
            //情况2： 预订失败    ? 是房间类型
            System.out.println("很抱歉，" + (roomType.equals("1") ? "单人间" : "双人间") + " 已全部预订完毕，请选择其他房型或稍后再试！");
        }
    }

    //退订房间
    public void cancelReservation(String orderNumber) {
        final boolean hasThisNumber = this.booking.keySet().contains(orderNumber);
        if (hasThisNumber) {
            // 存在这个订单号， 现在去修改房间状态和去掉订单号
            Room orderRoom = this.rooms.stream()
                    .filter(room -> room.getRoomNumber().equals(this.booking.get(orderNumber).get(0)))
                    .findFirst()
                    .orElse(null);

            orderRoom.setReserved(false);

            this.booking.remove(orderNumber);
            //情况1：退订成功
            System.out.println("退订成功！订单号为：" + orderNumber);
        } else {
            //情况2：无预订记录
            System.out.println("未找到对应的预订记录，请检查订单号是否正确！");
        }
    }

    // 已预订房间信息
    public void displayReservedRooms() {
        this.rooms.stream()
                .filter(Room::isReserved)
                .forEach(room -> {
                    System.out.print(room.printRoom());
                    this.booking.forEach((k, v) -> {
                        if (v.get(0).equals(room.getRoomNumber())) {
                            System.out.print(" --> 订单号: " + k + " 入住时间: " + v.get(1));
                        }
                    });
                });
    }

    // 生成订单编号
    public String createOrderNumber(int numberLength) {
        String codes = "0123456789abcdefghijklmnopqrstuvwxyz";
        Random r = new Random();

        while (true) {
            char[] arr = new char[numberLength];
            for (int i = 0; i < numberLength; i++) {

                arr[i] = codes.charAt(r.nextInt(codes.length()));
            }

            String createCode = new String(arr);

            if (!this.booking.keySet().contains(createCode)) {
                return createCode;
            }
        }
    }
}
